package com.wht.controller;

import com.wht.service.CrawlerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
public class WebController {

    @Autowired
    private CrawlerService crawlerService;

    @GetMapping("/test") @ResponseBody
    public String test() {
        return "test";
    }

    @GetMapping("/get")
    public String get() {
        return "get";
    }

    @GetMapping("/result/{startTime}/{endTime}")
    public String result(@PathVariable("startTime") String startTime,
                         @PathVariable("endTime") String endTime,
                         Model model) {
        model.addAttribute("startTime", startTime);
        model.addAttribute("endTime", endTime);
        List<List<String>> info = crawlerService.solve(startTime, endTime);
        if (info == null) {
            return "error";
        }
        model.addAttribute("info", info);
        return "result";
    }
}
